﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Guog.Common.API;

namespace Guog.Common
{
    public class WindowHelper
    {
        /// <summary>
        /// 桌面窗口句柄
        /// Window handle for the desktop
        /// </summary>
        private static IntPtr desktopHandle;
        /// <summary>
        /// Shell窗口句柄
        /// Window handle for the shell
        /// </summary>
        private static IntPtr shellHandle; //  

        /// <summary>
        /// 是否有全屏程序运行
        /// </summary>
        /// <returns></returns>
        public static bool ExistsFullScreenProcess()
        {
            //因为桌面窗口和Shell窗口也是全屏，要排除在其他全屏程序之外。 
            //取得桌面和Shell窗口句柄 
            desktopHandle = User32.GetDesktopWindow();
            shellHandle = User32.GetShellWindow();

            //取得前台窗口句柄并判断是否全屏 
            bool runningFullScreen = false;
            RECT appBounds = new RECT();
            Rectangle screenBounds;
            IntPtr hWnd;
            //取得前台窗口 
            hWnd = User32.GetForegroundWindow();
            if (hWnd != null && !hWnd.Equals(IntPtr.Zero))
            {
                //判断是否桌面或shell        
                if (!(hWnd.Equals(desktopHandle) || hWnd.Equals(shellHandle)))
                {
                    //取得窗口大小 
                    User32.GetWindowRect(hWnd, ref appBounds);
                    //判断是否全屏 
                    screenBounds = Screen.FromHandle(hWnd).Bounds;
                    if ((appBounds.Bottom - appBounds.Top) == screenBounds.Height && (appBounds.Right - appBounds.Left) == screenBounds.Width)
                        runningFullScreen = true;
                }
            }
            return runningFullScreen;
        }



        private const int SM_CXSCREEN = 0;
        private const int SM_CYSCREEN = 1;
        private static IntPtr HWND_TOP = IntPtr.Zero;
        private const int SWP_SHOWWINDOW = 64; // 0x0040

        /// <summary>
        /// 屏幕宽
        /// </summary>
        public static int ScreenWidth
        {
            get { return User32.GetSystemMetrics(SM_CXSCREEN); }
        }

        /// <summary>
        /// 屏幕高
        /// </summary>
        public static int ScreenHeight
        {
            get { return User32.GetSystemMetrics(SM_CYSCREEN); }
        }

        /// <summary>
        /// 设置全屏幕
        /// </summary>
        /// <param name="hwnd"></param>
        public static void SetWinFullScreen(IntPtr hwnd)
        {
            User32.SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenWidth, ScreenHeight, SWP_SHOWWINDOW);
        }
    }
}
